package com.macro.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.macro.enums.AppHttpCodeEnum;
import com.macro.mapper.UiGoodsSkuMapper;
import com.macro.pojos.dto.ResponseResult;
import com.macro.pojos.dto.UiGoodsSkuDto;
import com.macro.pojos.pojo.UiGoodsSkuStock;
import com.macro.service.UiGoodsSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UiGoodsSkuServiceImpl implements UiGoodsSkuService {

    @Autowired
    UiGoodsSkuMapper uiGoodsSkuMapper;

    @Override
    public ResponseResult querySku(String goodsSn) {
        LambdaQueryWrapper<UiGoodsSkuStock> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (goodsSn == null){
            lambdaQueryWrapper.eq(UiGoodsSkuStock::getGoodsSn,goodsSn);
        }
        lambdaQueryWrapper.eq(UiGoodsSkuStock::getIsDeleted,false);
        List<UiGoodsSkuStock> uiGoodsSkuList = uiGoodsSkuMapper.selectList(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(uiGoodsSkuList)){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        return ResponseResult.okResult(uiGoodsSkuList);
    }

    @Override
    public ResponseResult updateSku(List<UiGoodsSkuDto> uiGoodsSkuDtoList) {
        if (uiGoodsSkuDtoList == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        for (UiGoodsSkuDto uiGoodsSkuDto : uiGoodsSkuDtoList) {
            UiGoodsSkuStock uiGoodsSku = new UiGoodsSkuStock();
            BeanUtils.copyProperties(uiGoodsSkuDto,uiGoodsSku);
            uiGoodsSku.setUpdatedTime(new Date());
            int result = uiGoodsSkuMapper.updateById(uiGoodsSku);
            if (result !=0){
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.FAIL);
    }

}
